<template>
	<view class="vehicle-page">
		<!-- 自定义导航栏 -->
		<view class="nav-bar" :style="{ paddingTop: statusBarHeight + 'px' }">
			<view class="nav-content">
				<view class="nav-back" @click="goBack">
					<text class="back-icon">‹</text>
				</view>
				<text class="nav-title">我的车辆</text>
				<view class="nav-right">
					<text class="nav-icon">⋯</text>
					<text class="nav-icon">○</text>
				</view>
			</view>
		</view>

		<!-- 内容区域 -->
		<view class="content-area">
			<!-- 空状态 -->
			<template v-if="vehicleList.length === 0">
				<view class="empty-state">
					<view class="empty-illustration">
						<!-- 车辆插画区域 -->
						<view class="car-illustration">
							<!-- 这里可以放车辆插画图片，暂时用文字代替 -->
							<text class="car-icon">🚗</text>
						</view>
						<text class="empty-text">暂无车辆信息</text>
					</view>
				</view>
			</template>
			
			<!-- 有车辆列表 -->
			<template v-else>
				<view 
					class="vehicle-item" 
					v-for="(item, index) in vehicleList" 
					:key="index"
				>
					<image :src="item.image || '/static/车辆-01@3x.png'" class="vehicle-thumb" mode="aspectFill" />
					<view class="vehicle-info">
						<text class="vehicle-model">车型: {{ item.model }}</text>
						<text class="vehicle-plate">车牌: {{ item.plate }}</text>
						<view class="vehicle-actions">
							<view class="default-option" @click="setDefault(index)">
								<text class="default-text">设为默认车辆</text>
								<text class="checkbox" :class="{ checked: item.isDefault }">
									{{ item.isDefault ? '✓' : '○' }}
								</text>
							</view>
							<text class="delete-btn" @click="deleteVehicle(index)">删除</text>
						</view>
					</view>
				</view>
			</template>
		</view>

		<!-- 底部添加车辆按钮 -->
		<view class="footer-btn">
			<button class="add-btn" @click="addVehicle">
				<text class="plus-icon">+</text>
				<text class="add-text">添加车辆</text>
			</button>
		</view>
	</view>
</template>

<script setup>
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app';
	import {
		ref
	} from 'vue';

	// 状态栏高度
	const statusBarHeight = ref(0);

	// 车辆列表数据
	const vehicleList = ref([
		// 示例数据，实际应该从后端获取
		// {
		// 	model: '奔驰-GR',
		// 	plate: '豫M35468',
		// 	isDefault: true,
		// 	image: '/static/车辆-01@3x.png'
		// }
	]);

	// 返回上一页
	const goBack = () => {
		uni.navigateBack();
	};

	// 设为默认车辆
	const setDefault = (index) => {
		// 将所有车辆设为非默认
		vehicleList.value.forEach(item => {
			item.isDefault = false;
		});
		// 设置当前车辆为默认
		vehicleList.value[index].isDefault = true;
		// 保存到本地存储
		uni.setStorageSync('vehicleList', vehicleList.value);
		// 这里可以调用后端API保存默认车辆设置
		uni.showToast({
			title: '已设为默认车辆',
			icon: 'success'
		});
	};

	// 删除车辆
	const deleteVehicle = (index) => {
		uni.showModal({
			title: '提示',
			content: '确定要删除这辆车吗？',
			success: (res) => {
				if (res.confirm) {
					vehicleList.value.splice(index, 1);
					// 保存到本地存储
					uni.setStorageSync('vehicleList', vehicleList.value);
					// 这里可以调用后端API删除车辆
					uni.showToast({
						title: '删除成功',
						icon: 'success'
					});
				}
			}
		});
	};

	// 添加车辆
	const addVehicle = () => {
		uni.navigateTo({
			url: '/pages/my/AddVehicle'
		});
	};

	// 加载车辆列表
	const loadVehicleList = () => {
		// 从本地存储获取车辆列表
		const savedVehicles = uni.getStorageSync('vehicleList');
		if (savedVehicles && savedVehicles.length > 0) {
			vehicleList.value = savedVehicles;
		} else {
			vehicleList.value = [];
		}
	};

	onLoad(() => {
		// 获取状态栏高度
		const systemInfo = uni.getSystemInfoSync();
		statusBarHeight.value = systemInfo.statusBarHeight;
		
		// 从本地存储加载车辆列表
		loadVehicleList();
	});

	onShow(() => {
		// 从添加成功页面返回时，重新加载车辆列表
		loadVehicleList();
	});
</script>

<style scoped>
	.vehicle-page {
		background-color: #f5f5f5;
		min-height: 100vh;
		padding-bottom: 120rpx;
	}

	/* 导航栏 */
	.nav-bar {
		background-color: #007AFF;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 1000;
	}

	.nav-content {
		height: 44px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 30rpx;
	}

	.nav-back {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.back-icon {
		color: #fff;
		font-size: 48rpx;
		font-weight: 300;
		line-height: 1;
	}

	.nav-title {
		color: #fff;
		font-size: 36rpx;
		font-weight: 500;
		flex: 1;
		text-align: center;
	}

	.nav-right {
		display: flex;
		align-items: center;
		gap: 20rpx;
	}

	.nav-icon {
		color: #fff;
		font-size: 40rpx;
	}

	/* 内容区域 */
	.content-area {
		margin-top: calc(44px + var(--status-bar-height, 0px));
		background-color: #fff;
		min-height: calc(100vh - 44px - var(--status-bar-height, 0px) - 120rpx);
	}

	/* 空状态 */
	.empty-state {
		display: flex;
		align-items: center;
		justify-content: center;
		min-height: 60vh;
		padding: 60rpx;
	}

	.empty-illustration {
		text-align: center;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.car-illustration {
		width: 400rpx;
		height: 400rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-bottom: 40rpx;
	}

	.car-icon {
		font-size: 200rpx;
		opacity: 0.6;
	}

	.empty-text {
		font-size: 32rpx;
		color: #999;
	}

	/* 车辆列表 */
	.vehicle-item {
		display: flex;
		align-items: center;
		padding: 30rpx;
		border-bottom: 1px solid #f0f0f0;
		background-color: #fff;
		margin-bottom: 2rpx;
	}

	.vehicle-thumb {
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
		margin-right: 30rpx;
		flex-shrink: 0;
	}

	.vehicle-info {
		flex: 1;
		display: flex;
		flex-direction: column;
		gap: 16rpx;
	}

	.vehicle-model,
	.vehicle-plate {
		font-size: 32rpx;
		color: #333;
	}

	.vehicle-actions {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 10rpx;
	}

	.default-option {
		display: flex;
		align-items: center;
		gap: 10rpx;
	}

	.default-text {
		font-size: 28rpx;
		color: #333;
	}

	.checkbox {
		width: 40rpx;
		height: 40rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		color: #999;
	}

	.checkbox.checked {
		color: #ff9800;
		font-weight: bold;
	}

	.delete-btn {
		font-size: 28rpx;
		color: #f00;
	}

	/* 底部添加按钮 */
	.footer-btn {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 20rpx 30rpx;
		background-color: #fff;
		box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
	}

	.add-btn {
		width: 100%;
		height: 88rpx;
		background-color: #007AFF;
		color: #fff;
		border: none;
		border-radius: 44rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		gap: 10rpx;
		font-size: 32rpx;
		font-weight: 500;
	}

	.plus-icon {
		font-size: 40rpx;
		line-height: 1;
	}

	.add-text {
		font-size: 32rpx;
	}
</style>
